package appnumerico.web;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import appnumerico.code.IntervalosRaicesService;
import appnumerico.web.utils.WebUtils;
import appnumerico.web.utils.WebUtils.Funcion;

@Controller
@RequestMapping("/raices/*")
public class MetodosRaicesController {
	
	private IntervalosRaicesService intervalosRaicesService;
	
	@Resource
	public void setIntervalosRaicesService(IntervalosRaicesService intervalosRaicesService) {
		this.intervalosRaicesService = intervalosRaicesService;
	}
	
	@RequestMapping
	public String biseccionForm(ModelMap model) {
		return "/raices/biseccion";
	}
	
	@RequestMapping
	public void biseccion(ModelMap model, HttpSession session,
			@RequestParam long iter,
			@RequestParam double x0, 
			@RequestParam double x1,
			@RequestParam double tolerancia) {
		
		String fx = WebUtils.getFuncion(session, Funcion.FX);
		model.put("rr", intervalosRaicesService.biseccion(fx, iter, x0, x1, tolerancia));
	}
	
	@RequestMapping
	public String puntoFijoForm(ModelMap model) {
		return "/raices/puntoFijo";
	}
	
	@RequestMapping
	public void puntoFijo(ModelMap model, HttpSession session,
			@RequestParam long iter,
			@RequestParam double x, 
			@RequestParam double tolerancia) {
		
		String fx = WebUtils.getFuncion(session, Funcion.FX);
		String gx = WebUtils.getFuncion(session, Funcion.GX);
		model.put("rr", intervalosRaicesService.puntoFijo(fx, gx, iter, x, tolerancia));
	}
	
}
